package com.aijk.web.admin.controller.openapi;

import com.aijk.commons.mijo.utils.PojoUtil;
import com.aijk.ehm.api.EhmResidentApi;
import com.aijk.ehm.api.SemStepApi;
import com.aijk.ehm.model.EhmResident;
import com.aijk.ehm.model.SemStep;
import com.aijk.ehm.model.qo.EhmResidentQo;
import com.aijk.ehm.model.qo.SemStepQo;
import com.aijk.ehm.model.vo.EhmResidentVo;
import com.aijk.web.admin.controller.openapi.entity.ApiResponse;
import com.aijk.web.admin.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.*;

@Component
@Controller
public class SemController extends ApiBaseController {

    private static final Logger logger = LoggerFactory.getLogger(SemController.class);

    @Autowired
    private SemStepApi stepApi;

    @Autowired
    private EhmResidentApi ehmResidentApi;

    /**
     * 记步排名
     * @param resident
     * @return
     */
    @RequestMapping(path = "app/sem/step/top", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object companyList(@Valid EhmResident resident) {
        if(StringUtils.isEmpty(resident.getCompanyId()))
            return  new ApiResponse("-1","参数异常");
        SemStepQo stepQo= new SemStepQo();
        stepQo.setPageNo(1l);
        stepQo.setPageSize(10l);
        stepQo.setStartdate(DateUtil.getDateTime(DateUtil.dateFormate(new Date(),"yyyy-MM-dd")+" 00:00:00"));
        stepQo.setEnddate(DateUtil.getDateTime(DateUtil.dateFormate(new Date(),"yyyy-MM-dd")+" 23:59:59"));
        stepQo.setSortway(" step_num desc");
        List<SemStep> top10Alls = stepApi.page(stepQo).getList();
        stepQo.setCompanyId(resident.getCompanyId());
        List<SemStep> top10Companys  = stepApi.page(stepQo).getList();
        Map<String,List<SemStep>> stepMap =new HashMap<>();


        //个人排名
        int countAll = 0;
        int countCompany = 0;
        SemStepQo qo= new SemStepQo();
        qo.setStartdate(DateUtil.getDateTime(DateUtil.dateFormate(new Date(),"yyyy-MM-dd")+" 00:00:00"));
        qo.setEnddate(DateUtil.getDateTime(DateUtil.dateFormate(new Date(), "yyyy-MM-dd")+" 23:59:59"));
        qo.setResidentId(resident.getId());
        List<SemStep> sp = stepApi.list(qo);
        if (sp.size()>0 && sp != null){
            qo.setStepNum(sp.get(0).getStepNum());
            qo.setResidentId(null);
            countAll = stepApi.count(qo);
            qo.setCompanyId(resident.getCompanyId());
            countCompany = stepApi.count(qo);

            sp.get(0).setTop(countAll);
            List<SemStep> spcompany = new ArrayList<>();
            List<SemStep> semStepList = PojoUtil.convert(sp , SemStep.class);
            semStepList.get(0).setTop(countCompany);
            top10Alls.add(sp.get(0));
            top10Companys.add(semStepList.get(0));
        }

        EhmResidentQo ehmResidentQo = new EhmResidentQo();
        ehmResidentQo.setStatus((byte)10);
        List<EhmResidentVo> ehmResidentVoList = ehmResidentApi.list(ehmResidentQo);
        HashMap<Long , String> hashMap = new HashMap<>();
        for (EhmResidentVo vo : ehmResidentVoList){
            hashMap.put(vo.getId() , vo.getTitle());
        }

        for (SemStep all : top10Alls){
            if(hashMap.containsKey(all.getResidentId())){
                all.setUname(hashMap.get(all.getResidentId()));
            }
        }

        for (SemStep company : top10Companys){
            if(hashMap.containsKey(company.getResidentId())){
                company.setUname(hashMap.get(company.getResidentId()));
            }
        }

        stepMap.put("top10Alls",top10Alls);
        stepMap.put("top10Companys",top10Companys);

        ApiResponse response = new ApiResponse();
        response.setData(stepMap);
        return response;
    }

}
